package cn.aixuxi.security.controller;

import cn.aixuxi.security.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpSession;

@Controller
public class IndexController {

    @Autowired
    private SysUserService userService;

    @GetMapping("/login.ftl")
    public String login(){
        return "login";
    }

    @GetMapping({"/","/index"})
    public String index(HttpSession session){
        // 获取登录认证信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        // 获取登录用户名
        String username = authentication.getName();
        // 获取用户的昵称
        String nickname = userService.selectByUsername(username).getNickname();
        session.setAttribute("loginname",nickname);
        return "main";
    }
}
